import { getFolder } from '../util/get-folder.mjs'
import { mkdirActionHelper } from '../util/mkdir-action-helper.mjs'

export default {
  description: '创建页面',
  prompts: [
    {
      type: 'list',
      name: 'path',
      message: '请选择页面创建目录',
      choices: getFolder('src/modules'),
    },
    {
      type: 'list',
      name: 'model',
      message: '请选择模式(Options或者Composition)',
      choices: ['Options', 'Composition'],
    },
    {
      type: 'input',
      name: 'name',
      message: '请输入文件名(去除扩展名)',
      validate: (v) => {
        if (!v || v.trim === '') {
          return '文件名不能为空'
        } else if (v == 'list' || v == 'detail') {
          return 'list 和 detail 为保留关键字，无法直接创建，请通过创建标准模块进行生成'
        } else {
          return true
        }
      },
    },
    {
      type: 'confirm',
      name: 'mkdir',
      message: '是否创建文件夹',
      default: true,
    },
  ],
  actions: (data) => {
    const { targetDir, actions } = mkdirActionHelper(data)
    let cusActions =
      data.model === 'Options'
        ? [
            {
              type: 'add',
              path: `${targetDir}/{{name}}.vue`,
              templateFile: 'dev-kit/plop-templates/page/options-index-vue.hbs',
              data: {
                moduleName: `${data.name}`,
              },
            },
            {
              type: 'add',
              path: `${targetDir}/{{name}}.service.ts`,
              templateFile: 'dev-kit/plop-templates/page/options-index-service.hbs',
            },
            {
              type: 'add',
              path: `${targetDir}/{{name}}.definition.ts`,
              templateFile: 'dev-kit/plop-templates/page/options-index-definition.hbs',
            },
          ]
        : [
            {
              type: 'add',
              path: `${targetDir}/{{name}}.vue`,
              templateFile: 'dev-kit/plop-templates/page/composition-index-vue.hbs',
              data: {
                moduleName: `${data.name}`,
              },
            },
            {
              type: 'add',
              path: `${targetDir}/{{name}}.service.ts`,
              templateFile: 'dev-kit/plop-templates/page/composition-index-service.hbs',
            },
            {
              type: 'add',
              path: `${targetDir}/{{name}}.definition.ts`,
              templateFile: 'dev-kit/plop-templates/page/composition-index-definition.hbs',
            },
          ]

    actions.push(...cusActions)
    return actions
  },
}
